#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
角色基类
定义角色的基本属性和方法
"""

import uuid
import time
from typing import Dict, Any

class Character:
    """
    角色基类
    """
    
    def __init__(self, name: str, character_class: str = "战士"):
        """
        初始化角色
        
        Args:
            name: 角色名称
            character_class: 角色职业
        """
        self.id = str(uuid.uuid4())
        self.name = name
        self.character_class = character_class
        self.level = 1
        self.exp = 0
        self.exp_to_next_level = 100
        
        # 基础属性
        self.hp = 100
        self.max_hp = 100
        self.mp = 50
        self.max_mp = 50
        self.attack = 20
        self.defense = 10
        self.speed = 15
        
        # 挂机相关
        self.is_idle = False
        self.idle_start_time = None
        self.idle_location = None
        
        # 战斗相关
        self.is_in_battle = False
        self.battle_id = None
        
        # 创建时间
        self.created_at = time.time()
        
    def to_dict(self) -> Dict[str, Any]:
        """
        将角色数据转换为字典
        """
        return {
            'id': self.id,
            'name': self.name,
            'character_class': self.character_class,
            'level': self.level,
            'exp': self.exp,
            'exp_to_next_level': self.exp_to_next_level,
            'hp': self.hp,
            'max_hp': self.max_hp,
            'mp': self.mp,
            'max_mp': self.max_mp,
            'attack': self.attack,
            'defense': self.defense,
            'speed': self.speed,
            'is_idle': self.is_idle,
            'idle_start_time': self.idle_start_time,
            'idle_location': self.idle_location,
            'is_in_battle': self.is_in_battle,
            'battle_id': self.battle_id,
            'created_at': self.created_at
        }
    
    @classmethod
    def from_dict(cls, data: Dict[str, Any]) -> 'Character':
        """
        从字典创建角色对象
        """
        character = cls(data['name'], data['character_class'])
        character.id = data['id']
        character.level = data['level']
        character.exp = data['exp']
        character.exp_to_next_level = data['exp_to_next_level']
        character.hp = data['hp']
        character.max_hp = data['max_hp']
        character.mp = data['mp']
        character.max_mp = data['max_mp']
        character.attack = data['attack']
        character.defense = data['defense']
        character.speed = data['speed']
        character.is_idle = data['is_idle']
        character.idle_start_time = data['idle_start_time']
        character.idle_location = data['idle_location']
        character.is_in_battle = data['is_in_battle']
        character.battle_id = data['battle_id']
        character.created_at = data['created_at']
        return character
    
    def gain_exp(self, amount: int):
        """
        获得经验值
        """
        self.exp += amount
        while self.exp >= self.exp_to_next_level:
            self.level_up()
    
    def level_up(self):
        """
        升级
        """
        self.exp -= self.exp_to_next_level
        self.level += 1
        self.exp_to_next_level = int(self.exp_to_next_level * 1.2)
        
        # 升级时提升属性
        self.max_hp += 20
        self.max_mp += 10
        self.attack += 5
        self.defense += 3
        self.speed += 2
        
        # 恢复满血满蓝
        self.hp = self.max_hp
        self.mp = self.max_mp
        
        print(f"{self.name} 升级到 {self.level} 级!")
    
    def start_idle(self, location: str):
        """
        开始挂机
        """
        if self.is_in_battle:
            return False, "角色正在战斗中，无法挂机"
        
        self.is_idle = True
        self.idle_start_time = time.time()
        self.idle_location = location
        return True, f"{self.name} 开始在 {location} 挂机"
    
    def stop_idle(self):
        """
        停止挂机
        """
        if not self.is_idle:
            return False, "角色未在挂机状态"
        
        idle_duration = time.time() - self.idle_start_time
        self.is_idle = False
        self.idle_start_time = None
        location = self.idle_location
        self.idle_location = None
        
        return True, f"{self.name} 结束挂机，挂机时长: {idle_duration:.1f}秒"
    
    def get_idle_rewards(self) -> Dict[str, int]:
        """
        计算挂机奖励
        """
        if not self.is_idle or not self.idle_start_time:
            return {}
        
        idle_duration = time.time() - self.idle_start_time
        idle_hours = idle_duration / 3600
        
        # 基础奖励：每小时10经验
        base_exp_per_hour = 10
        exp_reward = int(idle_hours * base_exp_per_hour)
        
        return {
            'exp': exp_reward,
            'idle_duration': idle_duration
        }
    
    def __str__(self):
        return f"{self.name}(Lv.{self.level} {self.character_class})"